package com.franklin.maven.localcleaner.api.swing.tree;

import com.franklin.maven.localcleaner.api.swing.text.HighlightTextTextArea;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.util.List;

/**
 * @author Franklin
 * @since 2022/04/05 21:54
 */
public class HighlightTextTreeCellRenderer extends DefaultTreeCellRenderer {

    private final Color highlightColor;

    public HighlightTextTreeCellRenderer(Color highlightColor) {
        this.highlightColor = highlightColor;
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        if (!(value instanceof HighlightTextTreeNode)){
            return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        }
        HighlightTextTreeNode<?> highLightTextTreeNode = (HighlightTextTreeNode<?>) value;
        List<String> highlightTextList = highLightTextTreeNode.highlightTextList;
        return new HighlightTextTextArea(highLightTextTreeNode.icon,
                this.highlightColor,
                highlightTextList,
                highLightTextTreeNode.getName());
    }
}
